package nl.jworks.epub.organizer.routes;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.epub.EpubReader;
import org.apache.camel.Exchange;
import org.apache.camel.Message;

import javax.activation.DataHandler;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class MyProcessor {
    public void process(Exchange exchange) {
        try {
            System.out.println("process");
            Message in = exchange.getIn();
            byte[] file = in.getBody(byte[].class);
            System.out.println("bytes found:" + file.length);

            EpubReader epubReader = new EpubReader();
            Book book = epubReader.readEpub(new ByteArrayInputStream(file));
            //Book book = epubReader.readEpub(new FileInputStream("mybook.epub"));

            System.out.println("titles : " + book.getMetadata().getTitles());
            System.out.println("identifiers : " + book.getMetadata().getIdentifiers());

            String fileId = in.getHeader("CamelFileName", String.class);
            in.addAttachment(fileId, new DataHandler(file, "plain/text"));
        } catch (IOException e) {
            System.out.println("could not read file: " + e.getMessage());
        }

    }

}
